package com.xavier.book.spring_book_demo.service;

import com.xavier.book.spring_book_demo.enums.BookStatusEnums;
import com.xavier.book.spring_book_demo.mapper.BookInfoMapper;
import com.xavier.book.spring_book_demo.model.BookInfo;
import com.xavier.book.spring_book_demo.model.PageRequest;
import com.xavier.book.spring_book_demo.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.addBook(bookInfo);
    }

    public ResponseResult<BookInfo> getListMyPage(PageRequest pageRequest) {
        Integer count = bookInfoMapper.count();
        List<BookInfo> bookInfos = bookInfoMapper.selectBookByPage(pageRequest);
        for (BookInfo bookInfo : bookInfos) {
            BookStatusEnums statusEnums = BookStatusEnums.getStatusByCode(bookInfo.getStatus());
            bookInfo.setStatusCN(statusEnums == null ? "未知" : statusEnums.getDesc());
        }
        return new ResponseResult<>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {

       return bookInfoMapper.queryBookById(bookId);

    }

    public void updateBook(BookInfo bookInfo) {
        if (bookInfo == null || bookInfo.getId() == null || bookInfo.getId() <= 0) {
            throw new IllegalArgumentException("参数不合法：缺少有效的id");
        }
        bookInfoMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookInfoMapper.batchDeleteBook(ids);
    }

    public void deleteBookById(Integer id) {
        bookInfoMapper.deleteBookById(id);
    }
}
